package CuratorCtrl;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.SwingUtilities;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ImportInterface extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4744109466891800632L;
	private JLabel jLabel1;
	private JTextField tfCurName;
	private JPanel jPanel1;
	private JTextField tfMem;
	private JLabel jLabel3;
	private JPanel jPanel6;
	private JButton Path;
	private JTextField tfLoc;
	private JPanel jPanel5;
	private JPanel jPanel4;
	private JPanel jPanel3;
	private JLabel jLabel2;
	private JPanel jPanel2;
	private JButton Import;
	private JFileChooser fc=new JFileChooser(); ;


	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ImportInterface inst = new ImportInterface();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public ImportInterface() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			BoxLayout thisLayout = new BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS);
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setLocale(new java.util.Locale("en"));
			{
				jPanel1 = new JPanel();
				BoxLayout jPanel1Layout = new BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS);
				jPanel1.setLayout(jPanel1Layout);
				getContentPane().add(jPanel1);
				jPanel1.setBorder(new LineBorder(new java.awt.Color(0, 0, 0),
						1, false));
				jPanel1.setEnabled(false);
				jPanel1.setPreferredSize(new java.awt.Dimension(855, 184));
				jPanel1.setSize(855, 100);
				{
					jPanel3 = new JPanel();
					GridLayout jPanel3Layout = new GridLayout(1, 1);
					jPanel3Layout.setColumns(1);
					jPanel3Layout.setHgap(5);
					jPanel3Layout.setVgap(5);
					jPanel3.setLayout(jPanel3Layout);
					jPanel1.add(jPanel3);
					{
						jLabel1 = new JLabel();
						jPanel3.add(jLabel1);
						jLabel1.setText("Enter the Name of your new VM");
						jLabel1.setPreferredSize(new java.awt.Dimension(141, 29));
					}
					{
						tfCurName = new JTextField();
						jPanel3.add(tfCurName);
						tfCurName.setText("new_VM_name");
						tfCurName.setPreferredSize(new java.awt.Dimension(141, 29));
					}
				}
				{
					jPanel4 = new JPanel();
					GridLayout jPanel4Layout = new GridLayout(1, 1);
					jPanel4Layout.setColumns(1);
					jPanel4Layout.setHgap(5);
					jPanel4Layout.setVgap(5);
					jPanel4.setLayout(jPanel4Layout);
					jPanel1.add(jPanel4);
					{
						jLabel3 = new JLabel();
						jPanel4.add(jLabel3);
						jLabel3.setText("Memory Size(MB)");
					}
					{
						tfMem = new JTextField();
						jPanel4.add(tfMem);
						tfMem.setText("512");
						tfMem.setPreferredSize(new java.awt.Dimension(287, 32));
					}
				}
				{
					jPanel5 = new JPanel();
					BoxLayout jPanel5Layout = new BoxLayout(jPanel5, javax.swing.BoxLayout.X_AXIS);
					jPanel5.setLayout(jPanel5Layout);
					jPanel1.add(jPanel5);
					{
						Path = new JButton();
						jPanel5.add(Path);
						Path.setText("Select Target Path");
						Path.setPreferredSize(new java.awt.Dimension(79, 22));
						Path.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								PathActionPerformed(evt);
							}
						});
					}
					{
						tfLoc = new JTextField();
						jPanel5.add(tfLoc);
						tfLoc.setText("Location you want to installed");
						tfLoc.setPreferredSize(new java.awt.Dimension(255, 22));
					}
				}
				{
					jPanel6 = new JPanel();
					BoxLayout jPanel6Layout = new BoxLayout(jPanel6, javax.swing.BoxLayout.X_AXIS);
					jPanel6.setLayout(jPanel6Layout);
					jPanel1.add(jPanel6);
					{
						jLabel2 = new JLabel();
						jPanel6.add(jLabel2);
						jLabel2.setHorizontalAlignment(SwingConstants.LEFT);
						jLabel2.setPreferredSize(new java.awt.Dimension(285, 15));
						jLabel2.setText("Hello World from Curator,please set all the figures");
						//jLabel2.setText("Work Done,You can go to the manage/use interface to start it");
						
					}
				}
			}
			{
				jPanel2 = new JPanel();
				getContentPane().add(jPanel2);
				{
					Import = new JButton();
					jPanel2.add(Import);
					GridLayout ImportLayout1 = new GridLayout(1, 1);
					ImportLayout1.setColumns(1);
					ImportLayout1.setHgap(5);
					ImportLayout1.setVgap(5);
					GridLayout ImportLayout = new GridLayout(1, 1);
					ImportLayout.setHgap(5);
					ImportLayout.setVgap(5);
					ImportLayout.setColumns(1);
					Import.setText("Select image and Import");
					Import.setLayout(ImportLayout1);
					Import.setPreferredSize(new java.awt.Dimension(199, 25));
					Import.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							try {
								ImportActionPerformed(evt);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
				}
			}
			pack();
			this.setSize(435, 154);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	//handle
	
	private void ImportActionPerformed(ActionEvent evt) throws IOException {
		System.out.println("Import.actionPerformed, event=" + evt);
		// pass our @Para to our shell exec
		// UNDONE: store those info into a file.
		
		
		
		String path = "";
		String VMname=tfCurName.getText();
	    String targetPath=tfLoc.getText();

	//	int portnum=Integer.parseInt(tfPortNum.getText());
	//some checking process
		if(VMname.indexOf(' ')!=-1){
			JOptionPane.showMessageDialog(null,"no space in VM's name");
			return;
		}
		File file=new File(targetPath);
		if(!file.isDirectory()){
			JOptionPane.showMessageDialog(null,"Please select a valid target path to install");

			
		}
		
		
		Vector<String> exist=Util.read_rawlist();
		
		for(int i=0;i<exist.size();i++){
			if(exist.get(i).equals(VMname))
			{	
				JOptionPane.showMessageDialog(null,"Name already exist,choose a other name");

				return;
			}
		}
	      fc.setDialogTitle("Please select a OVA file");

	    if(evt.getSource()==Import){ 
	    
	    	      int intRetVal = fc.showOpenDialog(this); 

	    	      if( intRetVal == JFileChooser.APPROVE_OPTION){ 
	    	        path=fc.getSelectedFile().getPath(); 
	    	      } 
	    	 
	    
	    } 
	    if(!path.substring(path.length()-4).toLowerCase().equals(".ova")){
			JOptionPane.showMessageDialog(null,"Please select the ova file you download from curator_vm web page");
			return;
		}
	    
	    
	    
	    
	    jLabel2.setText("Importing VM,please wait...this may take long time");
		jLabel2.setForeground(new Color(255,0,0));
		
	    int mem=Integer.parseInt(tfMem.getText());
		//String osName = Util.get_os_name();
		Util.do_import_ge(VMname, path,0,mem,targetPath);
		jLabel2.setText("Work Done,You can go to the manage/use interface to start it");
		jLabel2.setForeground(new Color(0,0,0));
	}
	
	
	
	private void PathActionPerformed(ActionEvent evt) {
		System.out.println("Path.actionPerformed, event="+evt);
		String targetPath="";
		JFileChooser chooser = new JFileChooser(); 
		    chooser.setCurrentDirectory(new java.io.File("."));
		    chooser.setDialogTitle("select a target path you want to install the Curator");
		    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		    //
		    // disable the "All files" option.
		    //
		    chooser.setAcceptAllFileFilterUsed(false);
		    //    
		    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
		    	targetPath=chooser.getSelectedFile().getAbsolutePath();
		    	System.out.println("targetPath is :"+targetPath);
		    	tfLoc.setText(targetPath);
		    	tfLoc.setFocusable(false);
		    Import.requestFocus();
		      }
		    else {
		      System.out.println("No Selection ");
		      tfLoc.setFocusable(false);
			    Import.requestFocus();

		      }	}

}
